<template>
  <div role="tree" class="treeList" v-if="data.length > 0">
    <treeItem :node="node" v-bind="$attrs" v-on="$listeners" ref="tree" />
  </div>
</template>
<script>
import treeItem from "./tree-item";
export default {
  name: "mTree",
  components: {
    treeItem
  },
  props: {
    data: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      node: []
    };
  },
  computed: {},
  created() {
    if(this.data.length > 0){
      this.node = this.dealTreeData(this.data,1)
    }
  },
  mounted() {},
  watch: {
    data: {
      handler(val, oldVal) {
        this.node = this.dealTreeData(val, 1);
      },
      deep: true
    }
  },
  methods: {
    dealTreeData(tree, level) {
      let arr = [];
      tree.length &&
        tree.forEach((item, index) => {
          let obj = {};
          obj = this.deepCopy(item);
          obj.level = level;
          // obj.expanded = true;
          if (item.children && item.children.length) {
            obj.children = this.dealTreeData(item.children, level + 1);
          } else {
            if (!this.$attrs.lazy) {
              obj.isLeaf = true;
            } else {
              obj.isLeaf = false;
            }
          }
          arr.push(obj);
        });
      return arr;
    },
    deepCopy(obj) {
      var result = Array.isArray(obj) ? [] : {};
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
          if (typeof obj[key] === "object" && obj[key] !== null) {
            result[key] = this.deepCopy(obj[key]); //递归复制
          } else {
            result[key] = obj[key];
          }
        }
      }
      return result;
    },
    postData(data) {
      // this.$emit("load", data);
    }
  },
  destroyed() {}
};
</script>

<style lang="scss">
</style>
